// 登录
if (location.pathname.endsWith('login.html')) {
    document.getElementById('loginForm').onsubmit = function(e) {
        e.preventDefault();
        const username = document.getElementById('username').value.trim();
        const password = document.getElementById('password').value.trim();
        const userStr = localStorage.getItem('user_' + username);
        if (!userStr) {
            document.getElementById('loginMsg').textContent = '用户不存在';
            return;
        }
        const user = JSON.parse(userStr);
        if (user.password !== password) {
            document.getElementById('loginMsg').textContent = '密码错误';
            return;
        }
        localStorage.setItem('currentUser', username);
        location.href = '../index.html';
    };
}

// 注册
if (location.pathname.endsWith('register.html')) {
    document.getElementById('registerForm').onsubmit = function(e) {
        e.preventDefault();
        const username = document.getElementById('regUsername').value.trim();
        const password = document.getElementById('regPassword').value.trim();
        if (localStorage.getItem('user_' + username)) {
            document.getElementById('registerMsg').textContent = '用户名已存在';
            return;
        }
        localStorage.setItem('user_' + username, JSON.stringify({username, password}));
        document.getElementById('registerMsg').textContent = '注册成功，请登录';
        setTimeout(() => location.href = 'login.html', 1000);
    };
}

// 个人中心
function showProfile() {
    const user = localStorage.getItem('currentUser');
    if (!user) {
        document.getElementById('userInfo').innerHTML = '<p>请先登录</p>';
        return;
    }
    document.getElementById('userInfo').innerHTML = `<h3>用户名：${user}</h3>`;

    // 历史订单
    let orders = JSON.parse(localStorage.getItem('orders_' + user) || '[]');
    let html = '<h3>历史订单</h3>';
    if (orders.length === 0) {
        html += '<p>暂无历史订单</p>';
    } else {
        html += '<ul style="padding-left:20px;">';
        orders.forEach((order, idx) => {
            html += `<li>
                <b>订单${idx + 1}</b>（${order.time}）<br>
                ${order.items.map(item => `${item.name} x${item.quantity}`).join('，')}
                <br>总价：￥${order.total}
            </li>`;
        });
        html += '</ul>';
    }
    document.getElementById('orderHistory').innerHTML = html;
}

// 首页/导航栏登录状态联动
document.addEventListener('DOMContentLoaded', function() {
    const user = localStorage.getItem('currentUser');
    const loginLink = document.getElementById('loginLink');
    const registerLink = document.getElementById('registerLink');
    const profileLink = document.getElementById('profileLink');
    const logoutBtn = document.getElementById('logoutBtn');
    const welcomeUser = document.getElementById('welcomeUser');
    if (user) {
        if (loginLink) loginLink.style.display = 'none';
        if (registerLink) registerLink.style.display = 'none';
        if (profileLink) profileLink.style.display = '';
        if (logoutBtn) logoutBtn.style.display = '';
        if (welcomeUser) welcomeUser.textContent = '欢迎，' + user;
        if (logoutBtn) {
            logoutBtn.onclick = function() {
                localStorage.removeItem('currentUser');
                location.href = 'index.html';
            };
        }
    } else {
        if (loginLink) loginLink.style.display = '';
        if (registerLink) registerLink.style.display = '';
        if (profileLink) profileLink.style.display = 'none';
        if (logoutBtn) logoutBtn.style.display = 'none';
        if (welcomeUser) welcomeUser.textContent = '';
    }
});